<?php
set_include_path($_SERVER ['DOCUMENT_ROOT']);
include_once 'admin/includes/functions/admin-functions.php';
global $DBobject,$Cardclient;

$result = [
    'error' => null,
    'success' => true,
    'url' => '/admin/list/locations'
];

if (isset($_POST['type'])) {
    switch ($_POST['type']) {
        case 'location':
            $result['url'] = '/admin/list/locations';
            $data = [
                'userAccount' => 10000,
                'pageIndex'=>0,
                'pageSize'=>100,
            ];
            $response_data = $Cardclient->CallHttpPost('Get_ChainStorePagedV2', $data);
            if ($response_data['status']===0) {
                foreach ($response_data['data'] as $key => $value) {
                    $sql = "SELECT * FROM tbl_location WHERE location_object_id = :location_object_id LIMIT 1";
                    $params = ['location_object_id'=>$value['Guid']];
                    $location = $DBobject->wrappedSql($sql, $params);
                    if (is_array($location[0])) {
                        $sql = "UPDATE tbl_location SET location_name=:location_name,location_street=:location_street,location_suburb=:location_suburb,location_state=:location_state,location_country=:location_country,location_phone=:location_phone,location_contact=:location_contact,location_image=:location_image,location_description=:location_description WHERE location_object_id=:location_object_id";
                        $params = [
                            "location_object_id" => trim($value['Guid']),
                            "location_name" => $value['StoreName'],
                            "location_street" => $value['Address'],
                            "location_state" => $value['ProvinceName'],
                            "location_suburb" => $value['CityName'],
                            "location_country" => $value['CountyName'],
                            "location_phone" => $value['Tel'],
                            "location_contact" => $value['Contact'],
                            "location_image" => $value['ImagePath'],
                            "location_description" => $value['Description']
                        ];
                        $res = $DBobject->wrappedSql($sql, $params);
                        if (!$res) {
                            //wrong
                            $result['error'] = '同步出错，请重试。';
                            $result['success'] = false;
                            $result['url'] = null;
                        }
                    } else {
                        $sql = "INSERT INTO tbl_location (location_listing_id, location_object_id, location_name, location_street, location_suburb, location_state, location_country, location_phone, location_contact, location_image, location_description, location_published, location_created) VALUES (:location_listing_id,:location_object_id,:location_name,:location_street,:location_suburb,:location_state,:location_country,:location_phone,:location_contact,:location_image,:location_description,:location_published,:location_created)";
                        $params = [
                            "location_listing_id" => '9',
                            "location_object_id" => trim($value['Guid']),
                            "location_name" => $value['StoreName'],
                            "location_street" => $value['Address'],
                            "location_state" => $value['ProvinceName'],
                            "location_suburb" => $value['CityName'],
                            "location_country" => $value['CountyName'],
                            "location_phone" => $value['Tel'],
                            "location_contact" => $value['Contact'],
                            "location_image" => $value['ImagePath'],
                            "location_description" => $value['Description'],
                            "location_published" =>'1',
                            "location_created" => date('Y-m-d H:i:s')
                        ];
                        $res = $DBobject->wrappedSql($sql, $params);
                        $row_count = $DBobject->wrappedSqlRowcount();
                        if (!$res || $row_count!=1) {
                            //wrong
                            $result['error'] = '同步出错，请重试。';
                            $result['success'] = false;
                            $result['url'] = null;
                        }
                    }
                }
            }
            break;
        
        default:
            # code...
            break;
    }
}
echo json_encode($result);
die();
